{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "is_executing": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import sympy as sp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "339a42b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x\n",
      "a b c\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 符号定义\n",
    "# 定义单个符号\n",
    "x = sp.symbols('x')\n",
    "print(x)\n",
    "\n",
    "#定义多个符号\n",
    "a, b, c = sp.symbols('a b c')\n",
    "print(a, b, c)\n",
    "\n",
    "# 定义符号并指定它是实数\n",
    "y = sp.Symbol('y', real=True)   \n",
    "print(y.is_real)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "906d514f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a**2 + b**2 - c**2\n",
      "x**2 + 2*x*y + y**2\n",
      "0\n",
      "(x - 1)*(x + 1)\n"
     ]
    }
   ],
   "source": [
    "# 表达式的构建\n",
    "expr = a**2 + b**2 - c**2\n",
    "print(expr)\n",
    "\n",
    "# 将表达式展开\n",
    "expr_expanded = sp.expand((x+y)**2)\n",
    "print(expr_expanded)\n",
    "\n",
    "# 化简表达式\n",
    "simplified_expr = sp.simplify(x**2 + 2*x + 1 - (x + 1)**2)\n",
    "print(simplified_expr)  \n",
    "\n",
    "# 因式分解\n",
    "factored_expr = sp.factor(x**2 - 1)\n",
    "print(factored_expr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1e63b067",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Eq(x**2 + 2*x - 3, 0)\n",
      "解：[-3, 1]\n"
     ]
    }
   ],
   "source": [
    "# 方程的定义与求解\n",
    "# sp.Eq(lhs, rhs)：用于构建等式对象\n",
    "# sp.solve()：用于求解方程。 \n",
    "# 定义方程：x^2 + 2x - 3 = 0\n",
    "equation = sp.Eq(x**2 + 2*x - 3, 0)\n",
    "print(equation)\n",
    "\n",
    "# 求解方程\n",
    "solution = sp.solve(equation, x)\n",
    "print(f\"解：{solution}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "161a5149",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "替换 x = 5：42\n",
      "数值结果：42.0000000000000\n"
     ]
    }
   ],
   "source": [
    "# **表达式的替换与求值**\n",
    "# subs()：用于符号替换。\n",
    "# evalf()：用于数值求值。\n",
    "# 替换变量\n",
    "expr = x**2 + 3*x + 2\n",
    "new_expr = expr.subs(x, 5)\n",
    "print(f\"替换 x = 5：{new_expr}\")\n",
    "\n",
    "# 数值求值\n",
    "numeric_value = new_expr.evalf()\n",
    "print(f\"数值结果：{numeric_value}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
